package com.htc.model.utils;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import com.htc.common.ActionEvent;
import com.htc.constants.Constants;
import com.htc.controller.EventsController;
import com.htc.dlc.fragment.FragmentEvent;
import com.htc.model.dto.EventsDTO;
import com.htc.utils.CommonObjects;

public class EventsModel {
	public static EventsModel instance;

	public static EventsModel getInstance() {
		if (instance == null)
			instance = new EventsModel();
		return instance;
	}

	public void handleControllerEvent(ActionEvent e) {
		switch (e.action) {
		case Constants.GET_EVENTS:
			ArrayList<EventsDTO> news = getAllEvents((Object) e.sender);
			e.viewData = news;
			EventsController.getInstance().handleModelViewEvent(e);
			break;

		default:
			break;
		}
	}

	public ArrayList<EventsDTO> getAllEvents(Object ob) {
		ArrayList<EventsDTO> arrData = new ArrayList<EventsDTO>();
		String sql = "SELECT * from event_module";
		FragmentEvent fm = (FragmentEvent) ob;
		Context ctx = fm.getActivity().getBaseContext();
		DBHelper db = new DBHelper(ctx);
		Cursor c = db.Excutequery(sql);
		while (c.moveToNext()) {
			EventsDTO dto = new EventsDTO();
			dto.set_id(c.getString(c.getColumnIndex("event_id")));
			dto.setEventTitle(c.getString(c.getColumnIndex("event_title")));
			dto.setEventDescription(c.getString(c
					.getColumnIndex("event_description")));
			dto.setEventImage(c.getString(c.getColumnIndex("event_image")));
			dto.setEventDate(c.getString(c.getColumnIndex("added_date")));
			dto.setPriority(c.getString(c.getColumnIndex("priority")));
			dto.setEventMonth(c.getString(c.getColumnIndex("event_month")));
			dto.setEventYear(c.getString(c.getColumnIndex("event_year")));
			dto.setEventTitleAr(c.getString(c.getColumnIndex("event_title_ar")));
			dto.setEventDescriptionAr(c.getString(c
					.getColumnIndex("event_description_ar")));
			arrData.add(dto);
		}
		c.close();
		db.close();
		return arrData;
	}

	public boolean checkDB(Context context, String id) {
		boolean flag = false;
		String sql = "SELECT event_id FROM " + Constants.TBL_EVENT_MODULE
				+ " where event_id='" + id + "'";
		DBHelper db = DBHelper.getInstance(context);
		Cursor c = db.Excutequery(sql);
		while (c.moveToNext()) {
			flag = true;
			break;
		}
		c.close();
		db.close();
		return flag;
	}

	public boolean insertEvent(EventsDTO event, Context context) {
		boolean flag = false;
		if (!checkDB(context, event.get_id())) {
			String DATABASE_TABLE = Constants.TBL_EVENT_MODULE;
			ContentValues values = new ContentValues();
			values.put("event_id", event.get_id());
			values.put("event_title", event.getEventTitle());
			values.put("event_description", event.getEventDescription());
			values.put("event_image", event.getEventImage());
			values.put("added_date", event.getEventDate());
			values.put("priority", event.getPriority());
			values.put("event_title_ar", event.getEventTitleAr());
			values.put("event_description_ar", event.getEventDescriptionAr());
			values.put("event_month", "");
			values.put("event_year", "");
			DBHelper db = DBHelper.getInstance(context);
			flag = db.ExcuteInsert(null, DATABASE_TABLE, values);
			db.close();
		}

		return flag;
	}

	public boolean updateEvents(EventsDTO event, Context context) {
		boolean flag = false;

		String DATABASE_TABLE = Constants.TBL_EVENT_MODULE;
		String where = "event_id='" + event.get_id() + "'";
		ContentValues values = new ContentValues();
		
		values.put("event_title", event.getEventTitle());
		values.put("event_description", event.getEventDescription());
		values.put("event_image", event.getEventImage());
		values.put("added_date", event.getEventDate());
		values.put("priority", event.getPriority());
		values.put("event_title_ar", event.getEventTitleAr());
		values.put("event_description_ar ", event.getEventDescriptionAr());
		values.put("event_month", "");
		values.put("event_year", "");
		DBHelper db = DBHelper.getInstance(context);
		flag = db.ExcuteUpdate(null, DATABASE_TABLE, values, where, null);
		db.close();
		return flag;
	}

	public void deleteEvents(EventsDTO event, Context context) {
		String sql = "event_id='" + event.get_id() + "'";
		DBHelper db = DBHelper.getInstance(context);
		String DATABASE_TABLE = Constants.TBL_EVENT_MODULE;
		db.deleteTable(DATABASE_TABLE, sql);
		db.close();
	}
}
